import axios from 'axios'
import axiosFetch from '../../fetch/axios'

const state = {
    caseList: [],
    caseDetail: {}
}

const mutations = {
    setCaseList(state, data) {
        state.caseList = data
    },
    setCaseDetail(state, data) {
        state.caseDetail = data
    }
}

const actions = {

    getCaseList({ commit }, parmas) {
        return new Promise((resolve, reject) => {
            axios.get('/case/list', {　　
                params: { 'title': parmas }
            }).then(function(response) {
                if (response.data.Result === "100002") { //未登录
                    window.Wui.Toast.show(response.data.msg, 'middle');
                    setTimeout(function() {
                        location.href = '#/login';
                    }, 1500);
                    return false;
                }　
                commit('setCaseList', response.data);
            }).catch(function(error) {　　
                console.log(error);　
            });

        })
    },
    getCaseDetail({ commit }, id) {
        return new Promise((resolve, reject) => {
            axios.get('/case/detail', {　
                params: { 'id': id }
            }).then(function(response) {
                if (response.data.Result === "100002") { //未登录
                    window.Wui.Toast.show(response.data.msg, 'middle');
                    setTimeout(function() {
                        location.href = '#/login';
                    }, 1500);
                    return false;
                }　　
                commit('setCaseDetail', response.data);
                return resolve(response.data);
            }).catch(function(error) {　　
                console.log(error);　
                return reject(error)　
            });

        })
    },
    saveCase({ commit }, params) {
        return new Promise((resolve, reject) => {
            axiosFetch.fetchPost('/case/addcase', params).then(responseData => {
                return resolve(responseData);
            }, err => {
                return reject(err)
            });
        })
    },
    saveApply({ commit }, params) {
        return new Promise((resolve, reject) => {
            axiosFetch.fetchPost('/case/addapply', params).then(responseData => {
                return resolve(responseData);
            }, err => {
                return reject(err)
            });
        })
    },
    saveCaseAfter({ commit }, params) {
        return new Promise((resolve, reject) => {
            axiosFetch.fetchPost('/case/addcaseafter', params).then(responseData => {
                return resolve(responseData);
            }, err => {
                return reject(err)
            });
        })
    }
}


const getters = {
    caseList: state => state.caseList,
    caseDetail: state => state.caseDetail
}

export default {
    state,
    mutations,
    actions,
    getters
}